import logging
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
import time

class Base():
    # 封装初始化驱动的方法
    def __init__(self, driver):
        try:
            self.driver = driver
        except Exception as e:
            logging.info(e)

    # 封装打开网址的方法
    def open_url(self, url):
        try:
            self.driver.get(url)
        except Exception as e:
            logging.info(e)

    # 封装以时间取名的截图方法
    def time(self, name):
        try:
            t = time.strftime('%Y%m%d%H%M%S', time.localtime())
            self.driver.save_screenshot(t + name )
        except Exception as e:
            logging.info(e)

    # 封装显示等待寻找元素的方法
    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 20, 0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            self.time('jiuxian.png')
            print(e)

    # 封装点击事件的方法
    def click(self, loc):
        try:
            self.find_element(loc).click()
        except Exception as e:
            logging.info(e)

    # 封装键盘输入的方法
    def send_keys(self, loc, text):
        try:
            self.find_element(loc).send_keys(text)
        except Exception as e:
            logging.info(e)

    # 封装窗口切换的方法
    def window_handles(self):
        try:
            self.driver.switch_to.window(self.driver.window_handles[-1])
        except Exception as e:
            logging.info(e)

    # 封装打开iframe的方法
    def frame_open(self, loc):
        try:
            self.driver.switch_to.frame(self.find_element(loc))
        except Exception as e:
            logging.info(e)

    # 封装关闭iframe的方法
    def frame_close(self):
        try:
            self.driver.switch_to.default_content()
        except Exception as e:
            logging.info(e)

    # 封装键盘输入空格键的方法
    def keys_Space(self,loc):
        try:
            self.send_keys(loc,Keys.SPACE)
        except Exception as e:
            logging.info(e)

    # 封装键盘下一步的方法
    def keys_Enter(self,loc):
        try:
            self.send_keys(loc,Keys.ENTER)
        except Exception as e:
            logging.info(e)

    # 封装鼠标悬停的方法
    def move_to_element(self,loc):
        try:
            ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()
        except Exception as e:
            logging.info(e)

    # 封装鼠标双击的方法
    def double_click(self,loc):
        try:
            ActionChains(self.driver).double_click(self.find_element(loc)).perform()
        except Exception as e:
            logging.info(e)

    #封装滚动像素的方法
    def scrollTo(self,x):
        try:
            self.driver.execute_script(f'window.scrollTo(0,{x})')
        except Exception as e:
            logging.info(e)

    def select(self,loc,text):
        try:
            Select(self.find_element(loc)).select_by_visible_text(text)
        except Exception as e:
            logging.info(e)

